//
//  CEPDetailViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-7-2.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "CEPDetailViewController.h"
#import "UIView+WhenTappedBlocks.h"
#import "CEPDetailTimeCell.h"
#import "WebViewJavascriptBridge.h"
#import "CepService.h"

@interface CEPDetailViewController ()

@property (nonatomic, strong) IBOutlet UIView *shadeView;
@property (nonatomic, strong) IBOutlet UIView* activityView;
@property (nonatomic, strong) IBOutlet UITableView *infoListView;
@property(nonatomic,strong) IBOutlet UIWebView *webView;

@property NSInteger cepIndex;
@property (nonatomic, getter = isShowing) BOOL show;
@property WebViewJavascriptBridge* bridge;

@property (nonatomic,strong) Cep* cep;
@property (nonatomic,strong) NSArray*/*CepEvent*/ cepEvents;

@end

@implementation CEPDetailViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    //[self setupActivityView];
    
    [self setTitle:@"CEP详情"];
    
}

-(void)loadDataToView
{
    //读入html模板
    NSString *path = [[NSBundle mainBundle] pathForResource:@"webPage/cepdetail" ofType:@"html"];
    
    NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    
    NSString *string = [[NSString alloc] initWithData:
                        [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
    //设置内容
    NSString *pageTitle = self.cep.title;
    //NSString *bannerPath =  @"img/cepd@2x.jpg";
    //NSString *iconPath = self.cep.iconPath;
    NSString *title = self.cep.title;
    //NSString *subTitle = self.cep.title;
    NSString *dateTime = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"时间", nil),self.cep.displaytime];
    NSString *place = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"地点", nil),self.cep.place];
    NSString *content = self.cep.content;
    //NSString *followBtn = @"加关注";
    //根据关键字替换
    
    NSString *newPageTitle = [string stringByReplacingOccurrencesOfString:@"${pageTitle}" withString:pageTitle];
    //NSString *newBannerPath = [newPageTitle stringByReplacingOccurrencesOfString:@"${bannerPath}" withString:bannerPath];
    //NSString *newIconPath = [newBannerPath stringByReplacingOccurrencesOfString:@"${iconPath}" withString:iconPath];
    NSString *newTitle = [newPageTitle stringByReplacingOccurrencesOfString:@"${title}" withString:title];
    //NSString *newSubTitle = [newTitle stringByReplacingOccurrencesOfString:@"${subTitle}" withString:subTitle];
    NSString *newDateTime = [newTitle stringByReplacingOccurrencesOfString:@"${dateTime}" withString:dateTime];
    NSString *newPlace = [newDateTime stringByReplacingOccurrencesOfString:@"${place}" withString:place];
    NSString *newContent = [newPlace stringByReplacingOccurrencesOfString:@"${content}" withString:content];
    //NSString *newStr = [newContent stringByReplacingOccurrencesOfString:@"${followBtn}" withString:followBtn];
    
    //在webView上显示
    NSURL *mainBundleURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/webPage",[[NSBundle mainBundle] bundlePath]]];
    [self.webView loadHTMLString:newContent baseURL:mainBundleURL];
//
//    
//    _bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView handler:^(id data, WVJBResponseCallback responseCallback) {
//
//        if ([data isEqualToString:@"follow"])
//            [self showActivityView];
//    }];
    
    
    //[self.infoListView reloadData];
    
    
}

-(void)initWithCEPIndex:(NSInteger)index
{
    NSLog(@"initWithCEPIndex");
    self.cepIndex = index;
}

-(void)initWithCEP:(Cep *)cep
{
    self.cep = cep;
    [self loadDataToView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [HUD hide:YES];
    self.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}





@end
